package br.com.inner.modelo;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityManager;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Query;
import javax.persistence.Table;

import br.com.inner.custom.CustomException;
import br.com.inner.custom.JPAManager;

@Entity
@Table(name="UF")
@NamedQueries({
	@NamedQuery(name="UFPK",
			query="SELECT MAX(c.cdUf) FROM UF c"),
	@NamedQuery(name="UF",
			query="SELECT c FROM UF c WHERE c.cdUf = :cdUf")
})
public class UF implements Serializable{

	private static final long serialVersionUID = 1L;

	@Id
	@Column(name="cdUf", length=3)
	private BigDecimal cdUf; 
	
	@Column(name="dsUf", length=50)
	private String dsUf;
	
	@OneToMany(mappedBy="uf")
	List<Empresa> listEmpresa;

	public BigDecimal getCdUf() {
		return cdUf;
	}

	public void setCdUf(BigDecimal cdUf) {
		this.cdUf = cdUf;
	}

	public String getDsUf() {
		return dsUf;
	}

	public void setDsUf(String dsUf) {
		this.dsUf = dsUf;
	}
	
public void generatePK() throws CustomException {
		
		Query query = null;
		BigDecimal result = null;
		
		EntityManager manager = JPAManager.getEntityManager();
		query = manager.createNamedQuery("UFPK");
		result = (BigDecimal)query.getSingleResult();
		if (result == null) {
			result = new BigDecimal(1);
		}else{		
			result = result.add(new BigDecimal(1));
		}
		this.setPk(result);
	}
	
	public Object getPk() {
		return this.getCdUf();
	}
	
	public void setPk(Object newId) {
		// TODO Auto-generated method stub
	}

	public void setPk(BigDecimal _cdUf) {
		this.cdUf = _cdUf;
	}

	public int hashCode() {
		int hash = 0;
		hash += (this.cdUf != null ? this.cdUf.hashCode() : 0);
		return hash;
	}

	public boolean equals(Object object) {
		if (object == null || !this.getClass().equals(object.getClass())) {
			return false;
		}
		UF other = (UF) object;
		if (this.cdUf != other.cdUf
				&& (this.cdUf == null || !this.cdUf.equals(other.cdUf)))
			return false;
		return true;
	}
	
}
